//
//  LHDownloadCircleView.m
//  环形进度
//
//  Created by zhouLiHuan on 2017/2/14.
//  Copyright © 2017年 zhouLiHuan. All rights reserved.
//

#import "LHDownloadCircleView.h"

#define LineWidth 2;
@interface LHDownloadCircleView()

@property (nonatomic,assign)CGFloat startAngle; //开始的角度
@property (nonatomic,assign)NSInteger startRate; //开始比例
@property (nonatomic,assign)CGFloat vWidth;
@property (nonatomic,strong)CAShapeLayer *shapeLayer;
@property (nonatomic,strong)CADisplayLink *displayLink;
@property (nonatomic,strong)UIBezierPath *bezierPath;
@property (nonatomic,strong)UILabel *rateLabel;

@end

@implementation LHDownloadCircleView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.startAngle = -90;  //从圆的顶部开始
        self.startRate = 0;
        self.vWidth = frame.size.width;
        self.bezierPath = [UIBezierPath bezierPath];
        
        [self showNumLabel];
        [self backCircle]; //画底部的圆
        //配置CAShapelayer
        [self configShapeLayer];
        //配置CADisplayLink
        [self configDisplayLink];
    }
    return self;
}

#pragma mark - public methods
- (void)startAnimation{
    if (self.displayLink.paused == YES) {
        self.startAngle = -90;
        self.startRate = 0;
        self.displayLink.paused = NO;
    }
}

#pragma mark - UI相关的 private methods

//中间显示数字的label
- (void)showNumLabel{
    
    CGFloat X = 10;
    CGFloat W = self.frame.size.width - 2 * X;
    CGFloat H = 40;
    CGFloat Y = (self.frame.size.height - H) *0.5;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(X, Y, W, H)];
    self.rateLabel = label;
    self.rateLabel.textAlignment = NSTextAlignmentCenter;
    self.rateLabel.textColor = [UIColor blackColor];
    self.rateLabel.text = @"0%";
    [self addSubview:self.rateLabel];
    
}


//底部灰色的圆
- (void)backCircle{
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width * 0.5, self.frame.size.height) radius:self.vWidth * 0.5 startAngle:0 endAngle:360 clockwise:YES];
    CAShapeLayer *shaperLayer = [CAShapeLayer layer];
    shaperLayer.fillColor = [UIColor clearColor].CGColor;
    shaperLayer.lineWidth = LineWidth;
    shaperLayer.path = bezierPath.CGPath;
    [self.layer addSublayer:shaperLayer];
}

//配置 CAShaperLayer
- (void)configShapeLayer{
    self.shapeLayer = [CAShapeLayer layer];
    self.shapeLayer.strokeColor = [UIColor redColor].CGColor;
    self.shapeLayer.fillColor = [UIColor clearColor].CGColor;
    self.shapeLayer.lineWidth = self.vWidth;
    [self.layer addSublayer:self.shapeLayer];
}

#pragma mark - 配置CADisplayLink

- (void)configDisplayLink{
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawCircle)];
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    self.displayLink.paused = YES;
}

- (void)drawCircle{
    if (self.startRate >= self.rate) {
        self.bezierPath = [UIBezierPath bezierPath];
        self.displayLink.paused = YES;
        return;
    }
    
    self.startRate ++;
    self.rateLabel.text = [NSString stringWithFormat:@"%ld%%",self.startRate];
    [self.bezierPath addArcWithCenter:CGPointMake(self.frame.size.width *0.5, self.frame.size.height*0.5) radius:self.vWidth *0.5 startAngle:(M_PI/180.0)*self.startAngle endAngle:(M_PI/180.0)*(self.startAngle + 3.6) clockwise:YES];
    self.shapeLayer.path = self.bezierPath.CGPath;
    self.startAngle += 3.6;
}



#pragma mark - setter
- (void)setRate:(NSInteger)rate{
    if (rate <= 0 || rate > 100) {
        _rate = 100;
    }else{
        _rate = rate;
    }
}
 /*
 rate = 100;
*/

@end
